<template>
  <ant-modal modalWidth="1360" modalHeight="600" :visible="open" :modal-title="formTitle" :adjust-size="true" :maskClosable="false"
    @cancel="cancel">
    <a-form-model ref="form" :model="form" :rules="rules" slot="content" layout="vertical">
      <biz-product v-if="showProductModal" ref="bizProduct" @ok="selectProduct" @close="showProductModal = false" />
      <a-row :gutter="32">
        <a-col :span="12">
          <a-form-model-item label="采购单编号">
            <a-input v-model="form.purchaseCode" readOnly />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="供应商" prop="supplierCode">
            <a-select placeholder="请选择供应商" v-model="form.supplierCode" style="width: 100%" allow-clear  v-if="disabled" :disabled='disabled'>
              <a-select-option v-for="(d, index) in supplierOptions" :key="index" :value="d.supplierCode">{{
                d.supplierName }}</a-select-option>
            </a-select>
            <a-select placeholder="请选择供应商" v-model="form.supplierCode" style="width: 100%" allow-clear v-else>
              <a-select-option v-for="(d, index) in supplierOptions" :key="index" :value="d.supplierCode">{{
                d.supplierName }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="仓库" prop="warehouseId">
            <a-select v-model="form.warehouseId" style="width: 100%" allow-clear :disabled='disabled' v-if="disabled">
              <a-select-option v-for="(d, index) in warehouseIdOptions" :key="index" :value="d.id">{{ d.warehouseName
              }}</a-select-option>
            </a-select>
            <a-select v-model="form.warehouseId" style="width: 100%" allow-clear v-else>
              <a-select-option v-for="(d, index) in warehouseIdOptions" :key="index" :value="d.id">{{ d.warehouseName
              }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="部门" prop="deptId">
            <select-dept v-model="form.deptId" select-model="single" select-scope="all" v-if="disabled" :disabled='disabled'/>
            <select-dept v-model="form.deptId" select-model="single" select-scope="all" v-else/>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="采购日期" prop="purchaseDate">
            <a-date-picker v-model="form.purchaseDate" valueFormat="YYYY-MM-DD" :show-today="true" v-if="disabled" :disabled='disabled' style="width: 100%" />
            <a-date-picker v-model="form.purchaseDate" valueFormat="YYYY-MM-DD" :show-today="true" v-else style="width: 100%" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="备注" prop="remark">
            <a-input v-model="form.remark"  v-if="disabled" :disabled='disabled'/>
            <a-input v-model="form.remark"  v-else/>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-card :bordered="false">
            <advance-table :loading="loading" rowKey="id" size="middle" :columns="columns" :data-source="form.detailList"
              :pagination="false" :bordered="true">
              <div class="table-operations" slot="button" style='margin-bottom:0px;'>
                <div class="table_title">采购明细</div>
                <a-button type="primary" @click="handleAddProduct" >
                  <a-icon type="plus" />新增
                </a-button>
              </div>
              <span slot="unitSlot" slot-scope="{record}">
                <a-select placeholder="请选择单位" v-model="record.unit" :getPopupContainer="triggerNode => {
                    return triggerNode.parentNode || document.body
                  }
                  " style="width: 100%" allow-clear @change='changeUnit(record)'>
                  <a-select-option v-for="(d, index) in record.unitList" :key="index" :value="d.key">{{ d.name
                  }}</a-select-option>
                </a-select>
              </span>
              <span slot="spaceIdSlot" slot-scope="{record}">
                 <a-select placeholder="请选择仓位" v-model="record.spaceId" :getPopupContainer="triggerNode => {
                    return triggerNode.parentNode || document.body
                  }
                  " style="width: 100%" allow-clear @change='changeUnit(record)'>
                  <a-select-option v-for="(d, index) in spaceList" :key="index" :value="d.id">{{ d.postion
                    }}</a-select-option>
                </a-select>
              </span>
              <span slot="productNumberSlot" slot-scope="{record}">
                <a-input type='text' placeholder="请输入" v-model="record.productNumber" @change='changeUnit(record)' />
              </span>
              <span slot="unitPriceSlot" slot-scope="{record}">
                <a-input type='text' placeholder="请输入" v-model="record.unitPrice" @change='changeUnitPrice(record)'/>
              </span>
              <span slot="totalAmountSlot" slot-scope="{record}">
                <a-input type='text' placeholder="请输入" v-model="record.totalAmount" />
              </span>
              <span slot="remarkSlot" slot-scope="{record}">
                <a-input type='text' placeholder="请输入" v-model="record.remark" />
              </span>
              <span slot="operation" slot-scope="{record}">
                <a @click="handleDelDetail(record)">
                  删除
                </a>
              </span>

            </advance-table>
          </a-card>
        </a-col>
      </a-row>

    </a-form-model>
    <template slot="footer">
      <a-button type="primary" @click="modify" v-if="disabled == true">
        修改
      </a-button>
      <a-button type="primary" @click="submitForm(true)" v-if="disabled == false">
        保存
      </a-button>
      <a-button type="primary" @click="audit(form,2)" v-if="form.status == 1" v-hasPermi="['biz:bizPurchase:audit']">
        审核
      </a-button>
      <a-button type="primary" @click="audit(form,1)" v-if="form.status == 2" v-hasPermi="['biz:bizPurchase:audit']">
        反审
      </a-button>
      <a-button type="danger" @click="handleDelete(form)">
        删除
      </a-button>
      <a-button  @click="cancel">
        取消
      </a-button>
    </template>
  </ant-modal>
</template>
<script>
import BizPurchaseForm from './BizPurchaseForm'
export default {
  ...BizPurchaseForm
}
</script>
<style lang="less" scoped>
.table-operations,
body tr.ant-table-expanded-row:hover .table-operations {
  padding-bottom: 8px;
  text-align: right;
}

.table-operations {
  margin-bottom: 16px;
}

.table-operations>button,
.table-operations .ant-input-affix-wrapper {
  margin-right: 8px;
}

.table_title {
  float: left;
  padding-left: 8px;
  font-weight: 600;
  color: #222222;
  line-height: 30px;
}
</style>
